//
//  InvitationViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/8/3.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "InvitationViewController.h"

@interface InvitationViewController ()
@property (weak, nonatomic) IBOutlet UIView *shareBackView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *shareButtonBackViewBottomLayout;

@end

@implementation InvitationViewController
#pragma mark - IBAction
- (IBAction)invitationButtonAction:(id)sender {
    [self.shareBackView setHidden:NO];
    self.shareButtonBackViewBottomLayout.constant = 0;
    [UIView animateWithDuration:0.3 animations:^{
        [self.shareBackView setAlpha:1];
        [self.shareBackView layoutSubviews];
    } completion:^(BOOL finished) {
        
    }];
}
- (IBAction)weixinShareButtonAction:(id)sender {
    [self shareWebPageToPlatformType:UMSocialPlatformType_WechatSession];
}
- (IBAction)pyqShareButtonAction:(id)sender {
    [self shareWebPageToPlatformType:UMSocialPlatformType_WechatTimeLine];
}
- (IBAction)qzoneShareButtonAction:(id)sender {
    [self shareWebPageToPlatformType:UMSocialPlatformType_Qzone];
}
- (void)shareWebPageToPlatformType:(UMSocialPlatformType)platformType
{
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:URL_Download parameters:nil success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            //创建分享消息对象
            UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
            
            //创建网页内容对象
            //NSString* thumbURL = @"";//https://mobile.umeng.com/images/pic/home/social/img-1.png
            UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"分享未觉，分享快乐，快快加入！" descr:@"邀请好友一起加入未觉，享受一站购物的乐趣，与好友一起共同分享好商品，赶快邀请好友加入吧！" thumImage:[UIImage imageNamed:@"iTunesArtwork"]];
            //设置网页地址
            shareObject.webpageUrl = responseObject[@"data"][@"url"];
            
            //分享消息对象设置分享内容对象
            messageObject.shareObject = shareObject;
            
            //调用分享接口
            [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
                [self backViewTap:nil];
                if (error) {
                    UMSocialLogInfo(@"************Share fail with error %@*********",error);
                    NSLog(@"分享Error -- %@",error);
                }else{
                    if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                        UMSocialShareResponse *resp = data;
                        //分享结果消息
                        UMSocialLogInfo(@"response message is %@",resp.message);
                        //第三方原始返回的数据
                        UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                    }else{
                        UMSocialLogInfo(@"response data is %@",data);
                    }
                }
            }];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
    
}
#pragma mark - action
- (void)backViewTap:(UIGestureRecognizer *)tap {
    self.shareButtonBackViewBottomLayout.constant = -120;
    [UIView animateWithDuration:0.3 animations:^{
        [self.shareBackView setAlpha:0];
        [self.shareBackView layoutSubviews];
    } completion:^(BOOL finished) {
        [self.shareBackView setHidden:YES];
    }];
}
#pragma mark - UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"新人邀约";
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backViewTap:)];
    [self.shareBackView addGestureRecognizer:tap];
    [[UIApplication sharedApplication].keyWindow addSubview:_shareBackView];
    [_shareBackView setFrame:Screen];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
